import http from '@/public/request'

const keyHasSym: Array<string> = [
  'loadingPort',
  'receiptPlace',
  'transshipmentPort',
  'requester',
  'shipingAgent',
  'bookingParty',
  'bookingAgent'
]

//字段和搜索对应
const keyApiMap = new Map<string, Function>()

//需要下拉数据的对应key
let needKey: string[] = []
//需要下拉数据的对应请求
let needPro: any[] = []
//请求接口 用来和创建标准option
const REQUEST_URL = {
  //货物来源
  basedictdetail: '/api/basedictdetail/querypage',
  //船代
  crcontactcompany: '/api/crcontactcompany/query',
  //港口API
  port: '/api/csport/querypage',
  //船名（船舶信息）API
  CsShip: '/api/csship/querypage',
  // 航次
  CsVoyage: '/api/vsvoyage/querypage',
  // 挂港信息
  CsVoyagePort: '/api/csvoyageport/querypage',
  //包装单位API()
  CsPackageType: '/api/cspackagetype/querypage',
  //箱型箱尺
  csContainer: '/api/cscontainer/querypage',
  //船司信息
  CsShipCompany: '/api/csshipcompany/querypage',
  //海运航线
  tradelane: '/api/csportroute/querypage'
}

//分析有数据的key
function getHasDataKey(data: any) {
  for (let i of keyHasSym) {
    if (data[i]) {
      needKey.push(i)
      if (keyApiMap.get(i)) {
        needPro.push(keyApiMap.get(i)!(data[i]))
      }
    }
  }
}

const optionsStore = {
  state: {
    // 装货港
    loadingPortOption: [],
    // 卸货港
    dischargePortOption: [],
    // 卸货地点
    unloadingPlaceOption: [],
    // 船代
    shipingAgentOption: [],
    // 目的地
    finalDestination: []
  },
  mutations: {
    /**
     * 获取所有options
     */
    getAllOptions(data: object) {
      needKey = []
      needPro = []
      getHasDataKey(data)

      return Promise.allSettled(needPro)
    }
  }
}

export default optionsStore
